home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / game / misc / IChing.lha / IChing / IChing.c < prev    next >
C/C++ Source or Header  |  2001-07-16  |  19KB  |  836 lines

  1. /****h* IChing/IChing.c [2.0] *******************************************
  2. *
  3. * NAME
  4. *    IChing.c
  5. *
  6. * DESCRIPTION
  7. *    Take input coin tosses and convert to a hexagram.
  8. *
  9. * SYNOPSIS
  10. *    iching [t1 t2 t3 t4 t5 t6]
  11. *
  12. * INPUTS
  13. *    t1 - t6 = The number of heads in each 3-coin toss.
  14. *
  15. * HISTORY
  16. *    16-Jul-2001 Started a re-write of the entire program to use
  17. *                GadTools.
  18. *    23-Apr-1992
  19. *
  20. * NOTES
  21. *    $VER: IChing.c 2.0 (16-JUl-2001) by J.T. Steichen
  22. *************************************************************************
  23. *
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29.  
  30. #include <exec/types.h>
  31.  
  32. #include <AmigaDOSErrs.h>
  33.  
  34. #include <intuition/intuition.h>
  35. #include <intuition/classes.h>
  36. #include <intuition/classusr.h>
  37. #include <intuition/imageclass.h>
  38. #include <intuition/gadgetclass.h>
  39.  
  40. #include <libraries/gadtools.h>
  41.  
  42. #include <dos/dosextens.h>
  43. #include <dos/dos.h>
  44.  
  45. #include <graphics/displayinfo.h>
  46. #include <graphics/gfxbase.h>
  47.  
  48. #include <workbench/workbench.h>
  49. #include <workbench/startup.h>
  50. #include <workbench/icon.h>
  51.  
  52. #include <clib/exec_protos.h>
  53. #include <clib/dos_protos.h>
  54. #include <clib/intuition_protos.h>
  55. #include <clib/gadtools_protos.h>
  56. #include <clib/graphics_protos.h>
  57. #include <clib/utility_protos.h>
  58. #include <clib/diskfont_protos.h>
  59.  
  60. #include "IChing.h"
  61. #include "IChingStructs.h"
  62.  
  63. #include "CPGM:GlobalObjects/CommonFuncs.h"
  64.  
  65. // -------------------------------------------------------------------
  66.  
  67. IMPORT struct PAGE        *FirstPage;
  68. IMPORT struct IntuiText   *Attr, *FirstLine;
  69. IMPORT struct WBStartup   *_WBenchMsg;
  70.  
  71. // -------------------------------------------------------------------
  72.  
  73. PUBLIC struct IntuitionBase *IntuitionBase;
  74. PUBLIC struct GfxBase       *GfxBase;
  75. PUBLIC struct Library       *GadToolsBase;
  76. PUBLIC struct Library       *IconBase;
  77.  
  78. PUBLIC struct TextAttr      *Font   = NULL, FAttr = { 0, };
  79. PUBLIC struct CompFont       CFont  = { 0, };
  80. PUBLIC struct Screen        *Scr    = NULL;
  81. PUBLIC struct Window        *ICMWnd = NULL;
  82.  
  83. PUBLIC APTR                  VisualInfo = NULL;
  84.  
  85. PUBLIC int line_type1[6], line_type2[6];
  86.  
  87. // -------------------------------------------------------------------
  88.  
  89. PRIVATE char v[] = "\0$VER: IChing 2.0 " __AMIGADATE__ " by J.T. Steichen\0";
  90.  
  91.  
  92. PRIVATE char  TextFile[]       = "TEXTFILE";
  93. PRIVATE char  DefTextFile[128] = "IChing.txt";
  94. PRIVATE char *TTTextFile       = &DefTextFile[0];
  95.  
  96. PRIVATE struct DiskObject   *diskobj  = NULL;
  97. PRIVATE struct Menu         *ICMMenus = NULL;
  98. PRIVATE struct TextFont     *ICMFont  = NULL;
  99. PRIVATE struct IntuiMessage  ICMMsg;
  100.  
  101. PRIVATE UWORD  ICMLeft   = 0;
  102. PRIVATE UWORD  ICMTop    = 14;
  103. PRIVATE UWORD  ICMWidth  = 640;
  104. PRIVATE UWORD  ICMHeight = 200;
  105. PRIVATE UBYTE *ICMWdt    = "IChing -- The SoothSayer ©1992 by J.T. Steichen:";
  106.  
  107.  
  108. PRIVATE int ICMNewHexMI(  void );
  109. PRIVATE int ICMNextHexMI( void );
  110. PRIVATE int ICMQuitMI(    void );
  111.  
  112. PRIVATE struct NewMenu ICMNewMenu[] = {
  113.  
  114.    NM_TITLE, (STRPTR)"PROJECT", NULL, 0, NULL, NULL,
  115.   
  116.     NM_ITEM, (STRPTR) "New Hexagram",  (STRPTR) "N", 0, 0L, 
  117.     (APTR) ICMNewHexMI,
  118.  
  119.     NM_ITEM, (STRPTR) "Next Hexagram", (STRPTR) "H", 0, 0L, 
  120.     (APTR) ICMNextHexMI,
  121.     
  122.     NM_ITEM, (STRPTR) "Quit",          (STRPTR) "Q", 0, 0L, 
  123.     (APTR) ICMQuitMI,
  124.   
  125.    NM_END, NULL, NULL, 0, 0L, NULL 
  126. };
  127.  
  128. PRIVATE struct ColorSpec ScreenColors[] = {
  129.  
  130.     0, 0x00, 0x00, 0x00,  // Black
  131.     1, 0x0F, 0x0F, 0x0F,  // White
  132.     2, 0x0F, 0x00, 0x00,  // Red
  133.     3, 0x0F, 0x07, 0x01,  // Orange
  134.     4, 0x0E, 0x0E, 0x00,  // Yellow
  135.     5, 0x00, 0x0E, 0x00,  // Green
  136.     6, 0x00, 0x00, 0x0F,  // Blue
  137.     7, 0x0D, 0x00, 0x0D,  // Purple
  138.     8, 0x00, 0x0F, 0x0F,  // Cyan
  139.     9, 0x0F, 0x07, 0x07,  // Pink
  140.    10, 0x03, 0x0F, 0x08,  // Light Green
  141.    11, 0x0F, 0x00, 0x0F,  // Lavender
  142.    12, 0x08, 0x08, 0x08,  // Gray1
  143.    13, 0x04, 0x04, 0x04,  // Gray2
  144.    14, 0x00, 0x09, 0x03,  // Dark Green
  145.    15, 0x05, 0x08, 0x00,  // Brown
  146.    16, 0x00, 0x00, 0x00,
  147.    17, 0x0F, 0x0F, 0x00,
  148.    18, 0x00, 0x00, 0x00,
  149.    19, 0x0F, 0x00, 0x00,
  150.    20, 0x04, 0x04, 0x04,
  151.    21, 0x05, 0x05, 0x05,
  152.    22, 0x06, 0x06, 0x06,
  153.    23, 0x07, 0x07, 0x07,
  154.    24, 0x08, 0x08, 0x08,
  155.    25, 0x09, 0x09, 0x09,
  156.    26, 0x0A, 0x0A, 0x0A,
  157.    27, 0x0B, 0x0B, 0x0B,
  158.    28, 0x0C, 0x0C, 0x0C,
  159.    29, 0x0D, 0x0D, 0x0D,
  160.    30, 0x0E, 0x0E, 0x0E,
  161.    31, 0x0F, 0x0F, 0x0F,
  162.    ~0, 0x00, 0x00, 0x00 
  163. };
  164.  
  165. PRIVATE UWORD DriPens[] = { 0xFFFF, };
  166.  
  167. PRIVATE int   index1, index2;
  168. PRIVATE int   line_val1[6], line_val2[6], Trigram[4];
  169.  
  170. // -------------------------------------------------------------------
  171.  
  172. /* change # of heads to 6 -> 9 & assign YIN or YANG to type[]: */
  173.  
  174. SUBFUNC void  process_heads( int *type, int *value )
  175. {
  176.    int   i;
  177.  
  178.    for (i = 0; i < 6; i++) 
  179.       {
  180.       value[i] = 9 - value[i];
  181.       
  182.       if (value[i] == 7 || value[i] == 9)
  183.          type[i] = YANG;
  184.       else if (value[i] == 6 || value[i] == 8)
  185.          type[i] = YIN;
  186.       }
  187.  
  188.    return;
  189. }
  190.  
  191. /* set up next hexagram based on what the old hexagram is,
  192. ** then set up line_type2[].    
  193. */
  194.  
  195. SUBFUNC void  make_next_hexagram( void )
  196. {
  197.    int   i;
  198.  
  199.    for (i = 0; i < 6; i++) 
  200.       {
  201.       if (line_val1[i] == 6)
  202.          line_val2[i] = 9;
  203.       else if (line_val1[i] == 9)
  204.          line_val2[i] = 6;
  205.       else
  206.          line_val2[i] = line_val1[i];
  207.  
  208.       if (line_val2[i] == 7 || line_val2[i] == 9)
  209.          line_type2[i] = YANG;
  210.       else if (line_val2[i] == 6 || line_val2[i] == 8)
  211.          line_type2[i] = YIN;
  212.       }
  213.  
  214.    return;
  215. }
  216.  
  217. SUBFUNC int pow2( int power )    /* return:  2 ^ power */
  218. {
  219.    int   count, rval = 1;
  220.  
  221.    count = power;
  222.    if (power == 0)
  223.       return( rval );
  224.  
  225.    while (count-- > 0)
  226.       rval *= 2;
  227.  
  228.    return( rval );
  229. }
  230.  
  231. /* change the binary no. in type to a Hexadecimal index into
  232. ** the page table.
  233. */
  234.  
  235. SUBFUNC void  determine_index( int *index, int *type )
  236. {
  237.    int   i, pow2( int );
  238.  
  239.    *index = 0;
  240.  
  241.    for (i = 0; i < 6; i++)
  242.       {
  243.       if (type[i] == 1)
  244.          *index += pow2( i );
  245.       }
  246.  
  247.    return;
  248. }
  249.  
  250. /* determine what trigrams are represented by the line_typeX[] arrays: */
  251.  
  252. SUBFUNC void  get_trigrams( void )
  253. {
  254.    int   i;
  255.  
  256.    Trigram[0] = Trigram[1] = Trigram[2] = Trigram[3] = 0;
  257.  
  258.    for (i = 0; i < 3; i++)  
  259.       {
  260.       if (line_type1[i] == 1)
  261.          Trigram[0] += pow2( i );
  262.  
  263.       if (line_type1[i + 3] == 1)
  264.          Trigram[1] += pow2( i + 3 );
  265.  
  266.       if (line_type2[i] == 1)
  267.          Trigram[2] += pow2( i );
  268.  
  269.       if (line_type2[i + 3] == 1)
  270.          Trigram[3] += pow2( i + 3 );
  271.       }
  272.  
  273.    return;
  274. }
  275.  
  276. /* the no. of heads was not on the command line. */
  277.  
  278. SUBFUNC void enter_heads( void )
  279. {
  280.    int   count = 0;  char  buff[3];
  281.  
  282.    while (count < 6) 
  283.       {
  284.       fprintf( stderr, "\nEnter the number of heads for toss %d: ",
  285.                count + 1 );
  286.  
  287.       fflush( stderr );
  288.  
  289.       line_val1[count] = atoi( gets( buff ));
  290.  
  291.       fprintf( stderr, "\nValue received: %d\n", line_val1[count] );
  292.       count++;
  293.       }
  294.  
  295.    return;
  296. }
  297.  
  298. SUBFUNC void  DeallocateStrings( struct IntuiText *pgs )
  299. {
  300.    int   i;
  301.    
  302.    for (i = 0; i < 768; i++)
  303.       free( pgs[i].IText );
  304.  
  305.    return;
  306. }
  307.  
  308. /* draw a double dashed line for YIN: */
  309.  
  310. SUBFUNC void DrawYIN( int x, int y )
  311. {
  312.    Move( ICMWnd->RPort, x, y );
  313.    Draw( ICMWnd->RPort, x + 26, y );
  314.    Move( ICMWnd->RPort, x, y + 1 );
  315.    Draw( ICMWnd->RPort, x + 26, y + 1 );
  316.  
  317.    Move( ICMWnd->RPort, x + 39, y );
  318.    Draw( ICMWnd->RPort, x + 65, y );
  319.    Move( ICMWnd->RPort, x + 39, y + 1 );
  320.    Draw( ICMWnd->RPort, x + 65, y + 1 );
  321.  
  322.    return;
  323. }
  324.  
  325. /* draw a double solid line for YANG */
  326.  
  327. SUBFUNC void  DrawYANG( int x, int y ) 
  328. {
  329.    Move( ICMWnd->RPort, x, y );
  330.    Draw( ICMWnd->RPort, x + 65, y );
  331.    Move( ICMWnd->RPort, x, y + 1 );
  332.    Draw( ICMWnd->RPort, x + 65, y + 1 );
  333.  
  334.    return;
  335. }
  336.  
  337. /* display fortune text & hexagram */
  338.  
  339. SUBFUNC void  Display( int *hexagram, int index )
  340. {
  341.    struct   PAGE      *p     = (FirstPage + index);
  342.    struct   IntuiText *lines = (FirstLine + index * 12);
  343.    int                 line_num;
  344.  
  345.    (void) SetAPen( ICMWnd->RPort, 0 );             /* erase old stuff: */
  346.  
  347.    RectFill( ICMWnd->RPort, 3L, 11L, 640L, 200L );
  348.  
  349.    PrintIText( ICMWnd->RPort, Attr, 0, 0 );
  350.    PrintIText( ICMWnd->RPort, p->Top_Trigram->TriText, 100, 21 );
  351.    PrintIText( ICMWnd->RPort, p->Bot_Trigram->TriText, 100, 46 );
  352.    DrawImage(  ICMWnd->RPort, p->Top_Trigram->TriImage, 550, 12 );
  353.    DrawImage(  ICMWnd->RPort, p->Bot_Trigram->TriImage, 550, 39 );
  354.  
  355.    PrintIText( ICMWnd->RPort, p->HexName, 11, 55 );
  356.  
  357.    (void) SetAPen( ICMWnd->RPort, 1 );
  358.  
  359.    for (line_num = 0; line_num < 6; line_num++) 
  360.       {
  361.       if (hexagram[ line_num ] == YIN)
  362.          DrawYIN(  18, 48 - line_num * 5 ); /* draw the bottom line 1st */
  363.       else
  364.          DrawYANG( 18, 48 - line_num * 5 );
  365.       }
  366.  
  367.    for (line_num = 0; line_num < 12; line_num++)
  368.       PrintIText( ICMWnd->RPort, (lines + line_num),
  369.                   11, 68 + line_num * 10 );
  370.  
  371.    return;
  372. }
  373.  
  374. PRIVATE char  NIL_[ 81 ];
  375.  
  376. SUBFUNC char *ReadLine( FILE *fp )
  377. {
  378.    char  *string = &NIL_[0];
  379.    int   letter;
  380.    
  381.    while ((letter = getc( fp )) != '\n' && letter != EOF)
  382.       *string++ = letter;
  383.  
  384.    *string = '\0';
  385.  
  386.    return( &NIL_[0] );  
  387. }
  388.  
  389. SUBFUNC int ReadTextFile( char *filename ) /* Read the IChing.txt file. */
  390. {
  391.    extern struct IntuiText pg[];
  392.    
  393.    FILE  *infile;  /* struct IntuiText pg[ 64 * 12 ] is in IChingText.c */
  394.    int   i;
  395.    char  *temp, *s;
  396.    
  397.    if ((infile = fopen( filename, "r" )) == NULL)
  398.       return -1;
  399.    else  
  400.       {
  401.       for (i = 0; i < 768; i++)  // 64 * 12 = 768
  402.          {  
  403.          /* 64 hexagrams * 12 lines of text */
  404.          
  405.          temp = ReadLine( infile );
  406.          
  407.          if ((s = calloc( 1, 81 )) == NULL)
  408.             return( -1 );
  409.          
  410.          (void) strcpy( s, temp );
  411.  
  412.          pg[ i ].IText = s;
  413.          }
  414.           
  415.       fclose( infile );
  416.       return( 0 );
  417.       }
  418. }
  419.  
  420. // -------------------------------------------------------------------
  421.  
  422. PRIVATE int SetupScreen( void )
  423. {
  424.    if ((Scr = OpenScreenTags( NULL, 
  425.  
  426.                SA_Left,       0,
  427.                SA_Top,        0,
  428.                SA_Width,      640,
  429.                SA_Height,     480,
  430.                SA_Depth,      8,
  431.                SA_Colors,     &ScreenColors[0],
  432.                SA_Type,          CUSTOMSCREEN,
  433.                SA_DisplayID,  DEFAULT_MONITOR_ID | LORES_KEY,
  434.                SA_AutoScroll, TRUE,
  435.                SA_Overscan,   OSCAN_TEXT,
  436.                SA_Pens,          &DriPens[0],
  437.                SA_Title,      "IChing ©1992-2001:",
  438.                TAG_DONE )
  439.        
  440.        ) == NULL)
  441.        return( -1 );
  442.  
  443.    Font = &FAttr;
  444.  
  445.    ComputeFont( Scr, Font, &CFont, 0, 0 );
  446.  
  447.    if ((VisualInfo = GetVisualInfo( Scr, TAG_DONE )) == NULL)
  448.       return( -2 );
  449.  
  450.    return( 0 );
  451. }
  452.  
  453. PRIVATE void CloseDownScreen( void )
  454. {
  455.    if (VisualInfo != NULL) 
  456.       {
  457.       FreeVisualInfo( VisualInfo );
  458.       VisualInfo = NULL;
  459.       }
  460.  
  461.    if (Scr != NULL) 
  462.       {
  463.       CloseScreen( Scr );
  464.       Scr = NULL;
  465.       }
  466.  
  467.    return;
  468. }
  469.  
  470. PRIVATE void CloseICMWindow( void )
  471. {
  472.    if (ICMMenus != NULL) 
  473.       {
  474.       ClearMenuStrip( ICMWnd );
  475.       FreeMenus( ICMMenus );
  476.       ICMMenus = NULL;    
  477.       }
  478.  
  479.    if (ICMWnd != NULL) 
  480.       {
  481.       CloseWindow( ICMWnd );
  482.       ICMWnd = NULL;
  483.       }
  484.  
  485.    if (ICMFont != NULL) 
  486.       {
  487.       CloseFont( ICMFont );
  488.       ICMFont = NULL;
  489.       }
  490.  
  491.    return;
  492. }
  493.  
  494. // ------------------------------------------------------------------
  495.  
  496. PRIVATE int ICMNewHexMI( void )
  497. {
  498.    IMPORT int HandleICReq( int *, int * );
  499.    
  500.    if (HandleICReq( line_type1, line_type2 ) == TRUE) 
  501.       {
  502.       determine_index( &index1, line_type1 );
  503.       determine_index( &index2, line_type2 );
  504.      
  505.       Display( line_type1, index1 );
  506.       }
  507.  
  508.    return( TRUE );
  509. }
  510.  
  511. PRIVATE int ICMNextHexMI( void )
  512. {
  513.    Display( line_type2, index2 );
  514.  
  515.    return( TRUE );
  516. }
  517.  
  518. PRIVATE int ICMCloseWindow( void )
  519. {
  520.    CloseICMWindow();
  521.    return( FALSE );
  522. }
  523.  
  524. PRIVATE int ICMQuitMI( void )
  525. {
  526.    return( ICMCloseWindow() );
  527. }
  528.  
  529. // ------------------------------------------------------------------
  530.  
  531. PRIVATE int OpenICMWindow( void )
  532. {
  533.    UWORD wleft = ICMLeft, wtop = ICMTop, ww, wh;
  534.  
  535.    ComputeFont( Scr, Font, &CFont, ICMWidth, ICMHeight );
  536.  
  537.    ww = ComputeX( CFont.FontX, ICMWidth );
  538.    wh = ComputeY( CFont.FontY, ICMHeight );
  539.  
  540.    if ((wleft + ww + CFont.OffX + Scr->WBorRight) > Scr->Width) 
  541.       wleft = Scr->Width - ww;
  542.    
  543.    if ((wtop + wh + CFont.OffY + Scr->WBorBottom) > Scr->Height) 
  544.       wtop = Scr->Height - wh;
  545.  
  546.    if ((ICMFont = OpenDiskFont( Font )) == NULL)
  547.       return( -5 );
  548.  
  549.    if ((ICMMenus = CreateMenus( ICMNewMenu, GTMN_FrontPen,
  550.                                 0L, TAG_DONE )) == NULL)
  551.       return( -3 );
  552.  
  553.    LayoutMenus( ICMMenus, VisualInfo, TAG_DONE );
  554.  
  555.    if ((ICMWnd = OpenWindowTags( NULL,
  556.  
  557.                   WA_Left,         wleft,
  558.                   WA_Top,          wtop,
  559.                   WA_Width,        ww + CFont.OffX + Scr->WBorRight,
  560.                   WA_Height,       wh + CFont.OffY + Scr->WBorBottom,
  561.                   
  562.                   WA_IDCMP,        IDCMP_MENUPICK | IDCMP_CLOSEWINDOW
  563.                     | IDCMP_REFRESHWINDOW | IDCMP_VANILLAKEY,
  564.                   
  565.                   WA_Flags,        WFLG_CLOSEGADGET | WFLG_SMART_REFRESH
  566.                     | WFLG_ACTIVATE | WFLG_BORDERLESS,
  567.                   
  568.                   WA_Title,        ICMWdt,
  569.                   WA_CustomScreen, Scr,
  570.                   TAG_DONE )
  571.       ) == NULL)
  572.       return( -4 );
  573.  
  574.    SetMenuStrip( ICMWnd, ICMMenus );
  575.    GT_RefreshWindow( ICMWnd, NULL );
  576.  
  577.    return( 0 );
  578. }
  579.  
  580. PRIVATE int ICMVanillaKey( int whichkey )
  581. {
  582.    int rval = TRUE;
  583.    
  584.    switch (whichkey)
  585.       {
  586.       case 'n':
  587.       case 'N':
  588.          rval = ICMNewHexMI();
  589.          break;
  590.       
  591.       case 'h':
  592.       case 'H':
  593.          rval = ICMNextHexMI();
  594.          break;
  595.       
  596.       case 'q':
  597.       case 'Q':
  598.       case 'x':
  599.       case 'X':
  600.       case 'e':
  601.       case 'E':
  602.          rval = ICMQuitMI();
  603.          break;
  604.       }
  605.       
  606.    return( rval );
  607. }
  608.  
  609. PRIVATE int HandleICMIDCMP( void )
  610. {
  611.    struct IntuiMessage *m;
  612.    struct MenuItem     *n;
  613.    int                (*mfunc)( void );
  614.    BOOL                 running = TRUE;
  615.  
  616.    while (running == TRUE)
  617.       {
  618.       if ((m = GT_GetIMsg( ICMWnd->UserPort )) == NULL) 
  619.          {
  620.          (void) Wait( 1L << ICMWnd->UserPort->mp_SigBit );
  621.          continue;
  622.          }
  623.  
  624.        CopyMem( (char *) m, (char *) &ICMMsg, 
  625.                 (long) sizeof( struct IntuiMessage )
  626.               );
  627.  
  628.        GT_ReplyIMsg( m );
  629.  
  630.        switch (ICMMsg.Class) 
  631.           {
  632.           case IDCMP_REFRESHWINDOW:
  633.              GT_BeginRefresh( ICMWnd );
  634.              GT_EndRefresh( ICMWnd, TRUE );
  635.              break;
  636.  
  637.           case IDCMP_VANILLAKEY:
  638.              running = ICMVanillaKey( ICMMsg.Code );
  639.              break;
  640.              
  641.           case IDCMP_CLOSEWINDOW:
  642.              running = ICMCloseWindow();
  643.              break;
  644.  
  645.           case IDCMP_MENUPICK:
  646.             if (ICMMsg.Code != MENUNULL)
  647.                {
  648.                n = ItemAddress( ICMMenus, ICMMsg.Code );
  649.  
  650.                if (n == NULL)
  651.                   break;
  652.  
  653.                mfunc = (void *) (GTMENUITEM_USERDATA( n ));
  654.  
  655.                if (mfunc == NULL)
  656.                   break;
  657.  
  658.                running = mfunc();
  659.                }
  660. /*             
  661.              while (ICMMsg.Code != MENUNULL) 
  662.                 {
  663.                 n = ItemAddress( ICMMenus, ICMMsg.Code );
  664.                 mfunc = (void *) (GTMENUITEM_USERDATA( n ));
  665.                 running = mfunc();
  666.                 ICMMsg.Code = n->NextSelect;
  667.                 }
  668. */             
  669.              break;
  670.           }
  671.        }
  672.  
  673.    return( running );
  674. }
  675.  
  676. PRIVATE void ShowHelpReq( void )
  677. {
  678.    char msg[] =    
  679.  
  680.     "I Ching operating instructions:\n"
  681.     "From WorkBench, once the program is running, select the New Hexagram\n"
  682.     "menu entry & enter the # of heads for the 6 tosses of 3 coins,\n"
  683.     "then press the DONE! button on the requester to see your fortune.\n\n"
  684.     "From the CLI, either enter the 6 coin tosses as command arguments,\n"
  685.     "or wait for the program to prompt you for the 6 numbers from the\n"
  686.     "CLI you invoked I Ching from.\n\n"
  687.     "Next Hexagram menu entry will change 0 or 3-head line entries to the\n"
  688.     "opposite type of line; 1 & 2-head entries do not change.\n";
  689.  
  690.    UserInfo( msg, "I Ching Instructions:" );
  691.    
  692.    return;
  693. }
  694.  
  695. PRIVATE void *processToolTypes( char **toolptr )
  696. {
  697.    if (toolptr == NULL)
  698.       return( NULL );
  699.  
  700.    TTTextFile = GetToolStr( toolptr, TextFile,  DefTextFile );
  701.  
  702.    return( NULL );
  703. }
  704.  
  705. PRIVATE void ShutdownProgram( void )
  706. {
  707.    CloseICMWindow();
  708.    CloseDownScreen();
  709.    CloseLibs();
  710.  
  711.    return;
  712. }
  713.  
  714. PRIVATE int SetupProgram( void )
  715. {
  716.    if (OpenLibs() < 0)
  717.       return( -1 );
  718.  
  719.    if ((IconBase = OpenLibrary( ICONNAME, 0L )) == NULL)
  720.       {
  721.       CloseLibs();
  722.       return( -2 );
  723.       }
  724.       
  725.    if (SetupScreen() < 0)
  726.       {
  727.       ShutdownProgram();
  728.       return( -3 );
  729.       }
  730.       
  731.    if (OpenICMWindow() < 0)
  732.       {
  733.       ShutdownProgram();
  734.       return( -4 );
  735.       }
  736.  
  737.    return( 0 );
  738. }
  739.  
  740. PUBLIC int main( int argc, char **argv )
  741. {
  742.    extern struct IntuiText pg[];
  743.  
  744.    struct WBArg  *wbarg;
  745.    char         **toolptr = NULL;
  746.    int            i;
  747.  
  748.    if (SetupProgram() < 0)
  749.       return( IoErr() );
  750.       
  751.    if (argc > 0)    /* from CLI:       */
  752.       {
  753.       // We prefer to use the ToolTypes: 
  754.       (void) FindIcon( &processToolTypes, diskobj, argv[0] );
  755.       
  756.       if (argc == 1)
  757.          {
  758.          if (ReadTextFile( TTTextFile ) < 0)
  759.             {
  760.             fprintf( stderr, "Couldn't find %s file to read!\n\n", 
  761.                              TTTextFile 
  762.                    );
  763.  
  764.             ShutdownProgram();
  765.             return( ERROR_OBJECT_NOT_FOUND );
  766.             }
  767.             
  768.          ScreenToBack( Scr );
  769.          enter_heads();
  770.          ScreenToFront( Scr );
  771.          }
  772.       else if (argc == 7)
  773.          {
  774.          if (ReadTextFile( TTTextFile ) < 0)
  775.             {
  776.             fprintf( stderr, "Couldn't find %s file to read!\n\n", 
  777.                              TTTextFile 
  778.                    );
  779.  
  780.             ShutdownProgram();
  781.             return( ERROR_OBJECT_NOT_FOUND );
  782.             }
  783.    
  784.          for (i = 0; i < 6; i++)
  785.             line_val1[i] = atoi( argv[i + 1] );
  786.          }
  787.       else
  788.          {
  789.          fprintf( stderr, "USAGE: %s [h1 h2 h3 h4 h5 h6]\n", argv[0] );
  790.          ShutdownProgram();
  791.          return( ERROR_REQUIRED_ARG_MISSING );
  792.          }
  793.  
  794.       process_heads( &line_type1[0], &line_val1[0] );
  795.       make_next_hexagram();
  796.       determine_index( &index1, &line_type1[0] );
  797.       determine_index( &index2, &line_type2[0] );
  798.       get_trigrams();
  799.       }
  800.    else             /* from Workbench: */
  801.       {
  802.       wbarg   = &(_WBenchMsg->sm_ArgList[ _WBenchMsg->sm_NumArgs - 1 ]);
  803.       toolptr = FindTools( diskobj, wbarg->wa_Name, wbarg->wa_Lock );
  804.  
  805.       processToolTypes( toolptr );
  806.  
  807.       if (ReadTextFile( TTTextFile ) < 0)
  808.          {
  809.          fprintf( stderr, "Couldn't find %s file to read!\n\n", 
  810.                           TTTextFile 
  811.                 );
  812.  
  813.          ShutdownProgram();
  814.          return( ERROR_OBJECT_NOT_FOUND );
  815.          }
  816.       }
  817.     
  818.    SetNotifyWindow( ICMWnd );
  819.    
  820.    Display( &line_type1[0], index1 );
  821.  
  822.    ShowHelpReq();
  823.    
  824.    (void) HandleICMIDCMP();
  825.  
  826.    ShutdownProgram();
  827.    DeallocateStrings( pg );
  828.          
  829.    FreeDiskObject( diskobj );
  830.    CloseLibrary( IconBase );
  831.    
  832.    return( RETURN_OK );
  833. }
  834.  
  835. /* ------------------ END of IChing.c file! ----------------------- */
  836.